package com.ruoyi.pg.pensionProject.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.pg.pensionProject.domain.PgRoleProject;
import com.ruoyi.pg.pensionProject.vo.PgRoleProjectVo;
import com.ruoyi.pg.pensionProject.mapper.PgRoleProjectMapper;
import com.ruoyi.pg.pensionProject.service.IPgRoleProjectService;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 角色项目关联Service业务层处理
 *
 * @author Ming
 * @date 2025-09-09
 */
@Service
public class PgRoleProjectServiceImpl extends ServiceImpl<PgRoleProjectMapper, PgRoleProject> implements IPgRoleProjectService {

    @Override
    public TableDataInfo<PgRoleProjectVo> queryList(PgRoleProjectVo entity) {
        return PageUtils.buildDataInfo(this.baseMapper.queryList(PageUtils.buildPage(), entity));
    }

    @Override
    public List<PgRoleProjectVo> queryAll(PgRoleProjectVo entity) {
        return this.baseMapper.queryList(entity);
    }

    @Override
    public List<String> getIds(PgRoleProjectVo entity) {
        return baseMapper.getIds(entity);
    }

    @Override
    public List<Long> getProjectIds(List<Long> roleId) {
        return baseMapper.getProjectIds(roleId);
    }

    @Override
    public PgRoleProjectVo queryById(Long id) {
        return this.baseMapper.queryById(id);
    }

    @Override
    public void insertBatch(PgRoleProjectVo entity) {
        if (CollectionUtil.isNotEmpty(entity.getProjectIds())) {
            baseMapper.deleteByRoleIdInt(entity.getRoleId());
            for (String projectId : entity.getProjectIds()) {
                PgRoleProject pgRoleProject = new PgRoleProject();
                pgRoleProject.setRoleId(entity.getRoleId());
                pgRoleProject.setProjectId(Long.valueOf(projectId));
                baseMapper.insert(pgRoleProject);
            }
        }
    }
}
